<template>
  <div class="main">
    <div class="swiper-body">
      <div class="swiper-hot">
        <!-- 1 -->
        <div class="hot-top">
          <h2 class="hot-text"></h2>
        </div>
        <!-- 2 -->
        <div class="hot-main">
          <ul class="hot-list">
            <li class="active">为你推荐</li>
            <li>网络歌曲</li>
            <li>经典</li>
            <li>背景音乐</li>
            <li>官方歌单</li>
            <li>情歌</li>
          </ul>
        </div>
      </div>
    </div>
    <swiper
      class="swiper-container"
      :navigation="{
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      }"
      loop
      :allowTouchMove="(allowTouchMove = false)"
      :pagination="{
        clickable: false,
      }"
    >
      <div class="swiper-button-prev"></div>

      <swiper-slide
        class="swiper-slide"
        :key="index"
        v-for="(item, index) in songList"
      >
        <div class="hot-dvd-list">
          <ul class="dvd-list">
            <li class="dvd-item" v-for="key in item" :key="key.id">
              <div class="dvd-box">
                <div class="dvd-box-bottom">
                  <a @click.prevent="goPlayList(key.id)">
                    <img :src="key.picUrl" />
                    <i class="dvd-player"></i>
                    <i class="dvd-background"></i>
                  </a>
                </div>
                <h4>
                  <span>{{ key.name }}</span>
                </h4>
                <div class="dvd-author">
                  <a>播放量:{{ key.playCount }} 万 </a>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </swiper-slide>

      <!-- 下一页 -->
      <div class="swiper-button-next"></div>

      <!-- 如果需要分页器 -->
      <!-- <div class="swiper-pagination"></div> -->
      <!-- 如果需要导航按钮 左右箭头。如果放置在swiper-container外面，需要自定义样式。-->

      <!-- <div class="swiper-button-next"></div> -->
      <!-- 如果需要滚动条 -->
      <!-- <div class="swiper-scrollbar"></div> -->
    </swiper>
  </div>
</template>

<script>
export default {
  name: 'SongList',
}
</script>

<script setup>
import { Swiper, SwiperSlide } from 'swiper/vue'
import { getPersonalized } from '@/api/Home'

import SwiperCore, {
  Navigation,
  Pagination,
  Scrollbar,
  A11y,
  Autoplay,
} from 'swiper'

import 'swiper/swiper-bundle.css'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
SwiperCore.use([Navigation, Pagination, Scrollbar, A11y, Autoplay])

const limit = ref(25)
const songList = ref([])

onMounted(async () => {
  const result = await getPersonalized(limit.value)
  if ((result.code = 200)) {
    var arr1 = []
    var arr2 = []
    result.result.forEach((item, index) => {
      if (index % 5 == 0) {
        arr1.push(arr2)
        arr2 = []
      }
      arr2.push({
        id: item.id,
        name: item.name,
        picUrl: item.picUrl,
        playCount: Math.round(item.playCount / 10000),
      })
    })
  }
  arr1.push(arr2)
  arr1.shift()
  songList.value = arr1
})

const router = useRouter()
const goPlayList = id => {
  router.push({ name: 'PlayList', query: { playListId: id } })
}
</script>

<style scoped lang="less">
.main {
  width: 100%;
  height: 532px;
  background: url('../img/bg.png') 50% 0 repeat-x;
}
.swiper-body {
  width: 1200px;
  margin: 0px auto;
}
/* 1-start */

.swiper-hot {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .hot-top {
    margin-top: 50px;
    width: 100%;

    .hot-text {
      background-image: url('../img/index_tit5.png');
      background-size: 100%;
      display: block;
      margin: 0 auto;
      width: 196px;
      height: 40px;
      background-position: 0 0;
    }
  }
  .hot-main {
    margin-top: 24px;
    width: 100%;

    .hot-list {
      width: 100%;
      text-align: center;
      li {
        display: inline-block;
        font-size: 15px;
        margin: 0 24px;
        color: #333;
        cursor: pointer;
        &:hover {
          color: #31c27c;
        }
      }
      .active {
        color: #31c27c;
      }
    }
  }
}

.hot-dvd-list {
  overflow: hidden;
  margin: 0 auto;
  width: 1200px;
  box-sizing: border-box;
  .dvd-list {
    overflow: hidden;
    margin-top: 50px;
    margin-bottom: 35px;
  }
  .dvd-item {
    list-style: none;
    float: left;
    white-space: nowrap;
    margin-right: 10px;
  }
  .dvd-box {
    width: 228px;
    height: 309px;
    padding-right: 20px;
    box-sizing: border-box;
  }
  .dvd-box-bottom {
    width: 210px;
    height: 230px;
    margin-bottom: 15px;
    position: relative;
    overflow: hidden;
  }
  .dvd-box-bottom img {
    width: 210px;
    height: 230px;
    /* box-sizing: border-box; */
  }
  .dvd-author a {
    color: #999;
    display: inline-block;
    height: 22px;
    font-size: 14px;
  }

  .dvd-box h4 {
    font-weight: normal;
    font-size: 14px;
    height: 22px;
    span {
      display: inline-block;
      width: 210px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
  }
  .dvd-box h4:hover {
    color: #31c27c;
  }
  .dvd-player {
    position: absolute;
    width: 40px;
    height: 40px;
    left: 50%;
    top: 50%;
    border-radius: 50%;
    /* transform: translate(-50%,-50%); */
    background-image: url(../img/player.png);
    background-size: cover;
    background-position: center;
    /* border: none; */
    opacity: 0;
    margin-left: -30px;
    margin-top: -30px;
  }
  .dvd-box:hover .dvd-player {
    position: absolute;
    width: 60px;
    height: 60px;
    left: 50%;
    top: 50%;
    border-radius: 50%;
    /* margin-left: -30px;
  margin-top: -30px; */
    transform: translate(-50%, -50%);
    background-size: cover;
    background-position: center;
    opacity: 1;
    transform: scale(1.1);
    transition: all 0.7s;
  }
  .dvd-background {
    position: absolute;
    width: 210px;
    height: 230px;
    top: 0;
    left: 0;
    background-color: #000;
    z-index: -1;
  }
  .dvd-box:hover .dvd-box-bottom a img {
    transform: scale(1.1);
    transition: all 0.7s;
  }
  .dvd-box:hover .dvd-background {
    transition: all 0.7;
    opacity: 0.2;
    z-index: 1;
  }
}
/* 1-end */
</style>
